//package com.hmall.gateway.filter;
//
//
//import cn.hutool.core.text.AntPathMatcher;
//import com.alibaba.nacos.shaded.com.google.common.io.LineReader;
//import com.hmall.gateway.config.AuthProperties;
//import com.hmall.gateway.config.JwtProperties;
//import com.hmall.gateway.utils.JwtTool;
//import lombok.RequiredArgsConstructor;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.boot.context.properties.EnableConfigurationProperties;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//@Component
//@RequiredArgsConstructor
//@EnableConfigurationProperties(AuthProperties.class)
//public class AuthGlobalFilter implements GlobalFilter , Ordered {
//    private final AuthProperties authProperties;
////    private final JwtProperties jwtProperties;
//    private final AntPathMatcher antPathMatcher = new AntPathMatcher();
//    private final JwtTool jwtTool;
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        //1.获取请求信息，判断是不是需要登录的路径，如：登录和搜索不需要登录
//        ServerHttpRequest request = exchange.getRequest();
//        String path = request.getPath().toString();
//        if (isExclude(path)){
//            return chain.filter(exchange);
//        }
//
//        //2.获取请求头进行验证
//        String token = request.getHeaders().getFirst("Authorization");
//        if (token == null){
//            throw new RuntimeException("请登录");
//        }
//        try {
//            Long userId = jwtTool.parseToken(token);
//    //TODO  传给后端微服务
//            System.out.println("用户id："+userId);
//            //3.校验令牌成功放行将用户信息传给后端微服务，失败抛出异常401
//        } catch (Exception e) {
//           // 失败抛出异常401
//            ServerHttpResponse response = exchange.getResponse();
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
//
//        }
//
//        return chain.filter(exchange);
//    }
//
//    private boolean isExclude(String path) {
//        for (String excludePath : authProperties.getExcludePaths()) {
//            if(antPathMatcher.match(excludePath, path)){
//                return true;
//            }
//        }
//        return false;
//    }
//
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//}

package com.hmall.gateway.filter;

import com.hmall.gateway.config.AuthProperties;
import com.hmall.gateway.utils.JwtTool;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
@EnableConfigurationProperties(AuthProperties.class)
@RequiredArgsConstructor
public class AuthGlobalFilter implements GlobalFilter, Ordered {
    private final JwtTool jwtTool;
    private final AuthProperties authProperties;
    private final AntPathMatcher antPathMatcher = new AntPathMatcher();
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //1、判断是否要拦截
        ServerHttpRequest request = exchange.getRequest();
        if (isExclude(request.getPath().toString())) {
            //无需拦截；直接放行
            return chain.filter(exchange);
        }
        //2、获取token
        String token = request.getHeaders().getFirst("authorization");
        //3、校验token
        try {
            Long  userId = jwtTool.parseToken(token);

            System.out.println(" userId = " + userId);
            //4、 传递用户信息
            exchange.mutate().request(builder -> {
                builder.header("user-info",userId.toString());
            }).build();

        } catch (Exception e) {
            //token校验失败
            ServerHttpResponse response = exchange.getResponse();
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            return response.setComplete();
        }

        //5、放行
        return chain.filter(exchange);
    }

    private boolean isExclude(String path) {
        for (String excludePath : authProperties.getExcludePaths()) {
            if (antPathMatcher.match(excludePath, path)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
